package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.juneng.common.Condition;
import com.juneng.config.PageResult;
import com.juneng.entity.Teacher;
import com.juneng.mapper.TeacherMapper;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.Course;
import com.juneng.mapper.CourseMapper;
import com.juneng.service.ICourseService;
import com.juneng.dto.CourseDto;
import com.juneng.dto.CourseQueryDto;
import com.juneng.vo.CourseVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 课程信息 服务实现类
 *
 * @author zds
 * @since 2021-07-08
 */
@Service
@AllArgsConstructor
public class CourseServiceImpl implements ICourseService {

    protected CourseMapper courseMapper;

    protected TeacherMapper teacherMapper;

    @Override
    public IPage<CourseVo> page(CourseQueryDto dto) {
        //   IPage<BCommonFileVo> page = Condition.getPage(dto);
        //  return bCommonFileMapper.page(page,dto);
        IPage<CourseVo> page = Condition.getPage(dto);
//        List<Course> coursesNum = courseMapper.selectList(new QueryWrapper<>());
//        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
//        List<Course> courses = courseMapper.selectList(new QueryWrapper<>());
//        PageResult<Course> PageResult = new PageResult<>(dto.getPageNum(), dto.getPageSize(), courses, coursesNum.size());
        IPage<CourseVo> page1 = courseMapper.page(page, dto);
        List<CourseVo> records = page1.getRecords();
        for (CourseVo record : records) {
            record.setCourseImages(record.getCourseImage().split(","));
            if (record.getTeacherId() != null){
                Teacher teacher = teacherMapper.selectById(record);
                record.setTeacherName(teacher.getTeacherName());
                record.setTeacherId(teacher.getTeacherId());
            }

        }
        page1.setRecords(records);
        return page1;
    }
    //displayPosition
    @Override
    public List<Course> list(CourseQueryDto dto) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto, Course.class));
        return courseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(CourseDto dto) {
        return courseMapper.insert(BeanCopyUtils.copy(dto, Course.class));
    }

    @Override
    public Integer updateById(CourseDto dto) {
        return courseMapper.updateById(BeanCopyUtils.copy(dto, Course.class));
    }

    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return courseMapper.deleteBatchIds(toIntList);
    }

    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }
}
